#include "common_api.h"

#include "image.h"
/**
 * 从八位数据里面获得十六位的颜色
 * @param mode
 * @param str
 * @return
 */
uint16_t image_getcolor(uint8_t mode,uint8_t *str)
{
	uint16_t color;
	if(mode)
	{
		color=((uint16_t)*str++)<<8;
		color|=*str;
	}else
	{
		color=*str++;
		color|=((uint16_t)*str)<<8;
	}
	return color;	
}
/**
 *
 * @param xsta
 * @param ysta
 * @param width
 * @param height
 * @param scan
 * @param p
 */
void image_show(uint16_t xsta,uint16_t ysta,uint16_t width,uint16_t height,uint8_t scan,uint8_t *p)
{  
	uint32_t i;
	uint32_t len=0;

//	LCD_Scan_Dir(L2R_U2D);//设置方向
	LCD_Set_Window(xsta,ysta,width,height);
//	LCD_SetCursor(xsta,ysta);//设置光标

	ST7789_WriteRAM_Start();   	//开始写入GRAM
	len=width*height;			//写入的数据长度
	for(i=0;i<len;i++)
	{
		LCD_WriteRAM(image_getcolor(scan&(1<<4),p)); 
		p+=2;
	} 	  
//	LCD_Set_Window(0,0,lcddev.width,lcddev.height);  	    
}  

/**
 * 在指定位置显示一个图片，尺寸不能超屏幕，不能勾选高位在前，必须包含图片信息头数据
 * @param x
 * @param y
 * @param imgx 图片数据
 */
void image_display(uint16_t x,uint16_t y, uint8_t * imgx)
{
	HEADCOLOR *imginfo;
 	uint8_t ifosize=sizeof(HEADCOLOR);//得到HEADCOLOR结构体大小
	imginfo=(HEADCOLOR*)imgx;
 	image_show(x,y,imginfo->w,imginfo->h,imginfo->scan,imgx+ifosize);		
}

void Time_DisPlay(uint8_t ON)
{
    if(ON)
    {
        image_display(20, 22, (uint8_t *)gImage_time1);
    }
    else
    {
		LCD_Fill(20, 22, 48, 48, BACK_COLOR);
    }
}
void Speed_DisPlay(uint8_t ON)
{
    if(ON)
    {
        image_display(20, 120, (uint8_t *)gImage_speed);
    }
    else
    {
		LCD_Fill(20, 120, 48, 48, BACK_COLOR);
    }
}
//void Pressure_DisPlay(uint8_t ON)
//{
//    if(ON)
//    {
//        image_display(20, 0, (uint8_t *)gImage_Pressure);
//    }
//    else
//    {
//		LCD_Fill(20, 0, 64, 64, BACK_COLOR);
//    }
//}

void LCD_DisPlay_Init(void)
{
		u8 work_Ltime;
		u32 work_Htime;	
//		LCD_ShowxNum(120, 15, 0, 1, 48, 0x80,WHITE);  //高位
//		LCD_ShowChar(148, 15, '.', 48, WHITE);
//		LCD_ShowxNum(172, 15, 0, 1, 48, 0x80,WHITE);	//低位
//		LCD_ShowString(196, 27, 320, 32, 32, " MPa");	
		work_Htime = Monitor_Work_Time /10;		//高位
		work_Ltime = Monitor_Work_Time % 10;	//低位
		//计时  Monitor_Work_Time
		LCD_ShowxNum(92, 22, work_Htime, 5, 48, 0x80,WHITE); //高位补零
		LCD_ShowChar(212, 22, '.', 48,WHITE);
		LCD_ShowxNum(228, 22, work_Ltime, 1, 48, 0,WHITE); 
		LCD_ShowChar(262, 22, 'H', 48,WHITE);
	
		LCD_ShowString(92,70, 96, 48, 48, "CAN:");
		LCD_ShowChar(195, 70, 'Y', 48,GREEN);
		LCD_ShowChar(219, 70, 'e', 48,GREEN);
		LCD_ShowChar(243, 70, 's', 48,GREEN);	
	
		LCD_ShowxNum(92, 117, 0, 4, 48, 0x80,WHITE);	//转速
		LCD_ShowString(198, 117, 72, 48, 48, "RPM");
		
			
}

void LCD_DisPlay(void)
{
	uint8_t work_Ltime;
	u32 work_Htime;
	uint8_t temp = Flag_air +Flag_hand+Flag_fuel+Flag_water;

	//机油压力
//	if(Last_OilPressure != OilPressure)
//	{
//		Last_OilPressure = OilPressure;
//		if(OilPressure >= 950)
//		{
//			LCD_ShowxNum(120, 15, 1, 1, 48, 0x80,WHITE);  //高位
////			LCD_ShowChar(148, 15, '.', 48,WHITE);
//			LCD_ShowxNum(172, 15, 0, 1, 48, 0x80,WHITE);	//低位
//		}
//		else
//		{	
//			pretemp = OilPressure /100;
//			LCD_ShowxNum(120, 15, 0, 1, 48, 0x80,WHITE);  //高位
////			LCD_ShowChar(148, 15, '.', 48,WHITE);
//			LCD_ShowxNum(172, 15, pretemp, 1, 48, 0x80,WHITE);	//低位
//		}
//	}

	
	if(TimeCount)
	{		//计时信号让图标闪
			if(Flip_Flag ==1)
			{
				Time_DisPlay(1);
			}
			else
			{
				Time_DisPlay(0);
			}
			//判断计时的数值有没有更新
			if(Last_Worktime != Monitor_Work_Time)
			{	
				Last_Worktime = Monitor_Work_Time;			
				work_Htime = Monitor_Work_Time /10;	//高位
				work_Ltime = Monitor_Work_Time % 10;	//低位
				//计时  Monitor_Work_Time
				LCD_ShowxNum(92, 22, work_Htime, 5, 48, 0x80,WHITE); //高位补零
//				LCD_ShowChar(224, 80, '.', 48,WHITE);
				LCD_ShowxNum(228, 22, work_Ltime, 1, 48, 0,WHITE); 
//				LCD_ShowChar(272, 80, 'h', 48,WHITE);
			}
	
	}
	else
	{
		Time_DisPlay(1);
	}

	if(Last_Engine_Speed != Engine_Speed)
	{
		Last_Engine_Speed = Engine_Speed;	
		//转速
		LCD_ShowxNum(92, 117, Engine_Speed, 4, 48, 0x80,WHITE);
	}


	
	if( FaultCode != 0 )	//显示故障码
	{
		//SPN-FMI
		LCD_ShowString(0, 172, 320, 32, 33, "  Ecode: ");
		if(Flip_Flag )
		{
			LCD_ShowxNum(144, 172, FaultSPN_L, 6, 32, 0,WHITE);
			LCD_ShowChar(240,172,'-',32,WHITE);
			LCD_ShowxNum(256, 172, FaultFMI, 2, 32, 0,WHITE);
			LCD_ShowString(288, 172, 320, 32, 33, "    ");	
		}
		else
		{
			LCD_Fill(144, 172, 176, 32, BACK_COLOR);
		}
		

	}
	else if(( FaultCode == 0)&& (temp == 0 ))
	{	
			
			LCD_ShowString(0, 172, 320, 32, 32, "   Running Well              ");
					
	}
	else if (( FaultCode == 0)&& temp)
	{
//		LCD_ShowString(0, 200, 320, 32, 33, " air filter jammed  ");
//		LCD_ShowString(0, 200, 320, 32, 33, "  water temp high   ");
//		LCD_ShowString(0, 200, 320, 32, 33, "  fuel level low    ");
//		LCD_ShowString(0, 200, 320, 32, 33, " handbrake unlatched");
			static u8 count =0,temp_count =0; 
			static uint8_t temp_flag = 0 ;
			if(Flag_air&& (temp_flag&0x01)) 
			{
				if(Flip_Flag)
				{
					LCD_ShowString(0, 172, 320, 32, 33, " air filter jammed  ");
					temp_count ++;
				}
				else
				{
					LCD_Fill(0, 172, 320, 32, BACK_COLOR);
					temp_count++;
				}
				if(temp_count>=2)
				{
					count++;
					temp_count =0;
					temp_flag &=0x0E;
				}
			}
			else if(Flag_hand&& (temp_flag&0x02)) 
			{
				if(Flip_Flag)
				{
					LCD_ShowString(0, 172, 320, 32, 33, "handbrake unlatched ");
					temp_count ++;
				}
				else
				{
					LCD_Fill(0, 172, 320, 32, BACK_COLOR);
					temp_count ++;
				}
				if(temp_count>=2)
				{
					count++;;
					temp_count =0;
					temp_flag &=0x0D;
				}
			}
			else if(Flag_fuel&& (temp_flag&0x04)) 
			{
				if(Flip_Flag)
				{
					LCD_ShowString(0, 172, 320, 32, 33, "  fuel level low    ");
					temp_count ++;
				}
				else
				{
					LCD_Fill(0, 172, 320, 32, BACK_COLOR);
					temp_count ++;
				}
				if(temp_count>=2)
				{
					count++;;
					temp_count =0;
					temp_flag &=0x0B;
				}
			}
			else if(Flag_water&& (temp_flag&0x08)) 
			{
				if(Flip_Flag)
				{
					LCD_ShowString(0, 172, 320, 32, 33, "  water temp high     ");
					temp_count ++;
				}
				else
				{
					LCD_Fill(0, 172, 320, 32, BACK_COLOR);
					temp_count ++;
				}
				if(temp_count>=2)
				{
					count++;;
					temp_count =0;
					temp_flag &=0x07;
				}
			}
			
			if(count >= temp)
			{
				count = 0 ;
				temp_flag = Flag_Hex;
			}
			else if( temp_flag ==0)
			{
				temp_flag = Flag_Hex;
			}

			
	}
	
	if(TIME_CNT.no_can >= 5)
	{
		LCD_ShowChar(195, 70, 'N', 48,RED);
		LCD_ShowChar(219, 70, 'o', 48,RED);
		LCD_ShowChar(243, 70, ' ', 48,RED);
	}
	else
	{
		LCD_ShowChar(195, 70, 'Y', 48,GREEN);
		LCD_ShowChar(219, 70, 'e', 48,GREEN);
		LCD_ShowChar(243, 70, 's', 48,GREEN);
	}
}









